<script setup lang='ts'>
import { ref, watch } from 'vue';
import { doToDoFenMiao, duToDoFen, duFenMiaoToDu, duFenToDo } from "./base/duTransform"

type Number2 = [number, number]

const props = withDefaults(defineProps<{
    modelValue?: Number2,
    defaultValue?: Number2,
    withUndefined?: boolean
    readonly?: boolean
    propertyName?: string,
    lonLatFormat?: string,
    min?: number,
    max?: number,
}>(), { withUndefined: true, readonly: false, propertyName: '属性值', modelValue: () => [1, 1], lonLatFormat: '' })//lonLatFormat: 'SEXAGESIMAL_DEGREE' ||'DEGREES_DECIMAL_MINUTES'

const emits = defineEmits(["update:modelValue"]);
const timers = ref<NodeJS.Timeout>()

const inputHandler = (e: any, numberValues: Number2 | undefined, num: number) => {
    clearTimeout(timers.value)
    timers.value = setTimeout(() => {
        if (numberValues === undefined) return
        let numberArr = [...numberValues]
        numberArr[num] = Number(e.target.value)
        emits('update:modelValue', numberArr)
    }, 500);
}
const mouseFlag = ref(false)
const longitude = ref()
const latitude = ref()
//度分秒
const duFenMiaoLongitud = ref<[number, number, number]>([0, 0, 0])//经度
const duFenMiaoLatitude = ref<[number, number, number]>([0, 0, 0])//纬度
//度分
const duFenLongitude = ref<[number, number]>([0, 0])//经度
const duFenLatitude = ref<[number, number]>([0, 0])//纬度
watch(() => props.modelValue, (val) => {
    mouseFlag.value = false
    if (props.lonLatFormat === 'SEXAGESIMAL_DEGREE') {
        duFenMiaoLongitud.value = doToDoFenMiao(val[0])
        duFenMiaoLatitude.value = doToDoFenMiao(val[1])
    } else if (props.lonLatFormat === 'DEGREES_DECIMAL_MINUTES') {
        duFenLongitude.value = duToDoFen(val[0])
        duFenLatitude.value = duToDoFen(val[1])
    } else {
        longitude.value = val[0].toFixed(7)
        latitude.value = val[1].toFixed(7)
    }
}, { immediate: true })
const update = (e: any, numberValues: Number2 | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    let targetValue
    targetValue = (e.target as HTMLInputElement).value as any;
    let min
    let max
    if (num === 0) {
        min = -180
        max = 180
    } else if (num === 1) {
        min = -90
        max = 90
    }
    if (min !== undefined) {
        if (targetValue < min) {
            targetValue = min
        }
    }
    if (max !== undefined) {
        if (targetValue > max) {
            targetValue = max
        }
    }
    numberArr[num] = Number(targetValue)
    emits("update:modelValue", numberArr)
}
//度分秒
const update2 = (numberValues: Number2 | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    if (num === 0) {
        const du = duFenMiaoToDu(duFenMiaoLongitud.value)
        numberArr[num] = du
    } else if (num === 1) {
        const du = duFenMiaoToDu(duFenMiaoLatitude.value)
        numberArr[num] = du
    }
    emits("update:modelValue", numberArr)
}
//度分
const update3 = (numberValues: Number2 | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    if (num === 0) {
        const du = duFenToDo(duFenLongitude.value)
        numberArr[num] = du
    } else if (num === 1) {
        const du = duFenToDo(duFenLatitude.value)
        numberArr[num] = du
    }
    emits("update:modelValue", numberArr)
}
const resetDefult = () => {
    if (props.defaultValue) {
        const num = [props.defaultValue[0], props.defaultValue[1]]
        emits('update:modelValue', num)
    }
}
// border-top: 1px solid #111111;
//     border-bottom: 1px solid #111111;
//     padding-top: 10px;
//     padding-bottom: 10px;
</script>

<template>
    <div class="number2_prop"
        :style="{ padding: propertyName === '二维坐标' ? '10px 0' : '0', borderBottom: propertyName === '二维坐标' ? '1px solid #111111' : 'none', borderTop: propertyName === '二维坐标' ? '1px solid #111111' : 'none' }"
        v-if="propertyName === '二维坐标'">
        <div class="position_name">{{ '经纬度' }}</div>
        <div v-if="lonLatFormat === 'SEXAGESIMAL_DEGREE'" class="number2_dufenmiao">
            <div class="number2_dufenmiao_index" style="margin-bottom: 13px;">
                <div class="number2_dufenmiao_index_jingdu">
                    <div class="number2_div"><input type="number" class="input_number" v-model="duFenMiaoLongitud[0]"
                            @blur="update2(modelValue, 0)" @keydown.enter="update2(modelValue, 0)"
                            placeholder="请输入" /><span>{{ '°' }}</span>
                    </div>

                    <div class="number2_div"> <input type="number" class="input_number" v-model="duFenMiaoLongitud[1]"
                            @blur="update2(modelValue, 0)" @keydown.enter="update2(modelValue, 0)" placeholder="请输入" />
                        <span>{{ "'" }}</span>
                    </div>

                    <div class="number2_div"><input type="number" class="input_number" v-model="duFenMiaoLongitud[2]"
                            @blur="update2(modelValue, 0)" @keydown.enter="update2(modelValue, 0)"
                            placeholder="请输入" /><span>{{ '"' }}</span>
                    </div>

                </div>
            </div>
            <div class="number2_dufenmiao_index" style="margin-bottom: 6px;">
                <div class="number2_dufenmiao_index_jingdu">
                    <div class="number2_div"><input type="number" class="input_number" v-model="duFenMiaoLatitude[0]"
                            @blur="update2(modelValue, 1)" @keydown.enter="update2(modelValue, 1)"
                            placeholder="请输入" /><span>{{ '°' }}</span>
                    </div>

                    <div class="number2_div"> <input type="number" class="input_number" v-model="duFenMiaoLatitude[1]"
                            @blur="update2(modelValue, 1)" @keydown.enter="update2(modelValue, 1)"
                            placeholder="请输入" /><span>{{ "'" }}</span>
                    </div>

                    <div class="number2_div"><input type="number" class="input_number" v-model="duFenMiaoLatitude[2]"
                            @blur="update2(modelValue, 1)" @keydown.enter="update2(modelValue, 1)"
                            placeholder="请输入" /><span>{{ '"' }}</span>
                    </div>

                </div>
            </div>
        </div>
        <div v-else-if="lonLatFormat === 'DEGREES_DECIMAL_MINUTES'" class="number2_dufenmiao">
            <div class="number2_dufenmiao_index" style="margin-bottom: 13px;">
                <div class="number2_dufenmiao_index_jingdu">
                    <div class="number2_div"><input type="number" class="input_number" v-model="duFenLongitude[0]"
                            @blur="update3(modelValue, 0)" @keydown.enter="update3(modelValue, 0)"
                            placeholder="请输入" /><span>{{ '°' }}</span>
                    </div>

                    <div class="number2_div"> <input type="number" class="input_number" v-model="duFenLongitude[1]"
                            @blur="update3(modelValue, 0)" @keydown.enter="update3(modelValue, 0)"
                            placeholder="请输入" /><span>{{ "'" }}</span>
                    </div>

                </div>
            </div>
            <div class="number2_dufenmiao_index" style="margin-bottom: 6px;">
                <div class="number2_dufenmiao_index_jingdu">
                    <div class="number2_div"><input type="number" class="input_number" v-model="duFenLatitude[0]"
                            @blur="update3(modelValue, 1)" @keydown.enter="update3(modelValue, 1)"
                            placeholder="请输入" /><span>{{ '°' }}</span>
                    </div>

                    <div class="number2_div"> <input type="number" class="input_number" v-model="duFenLatitude[1]"
                            @blur="update3(modelValue, 1)" @keydown.enter="update3(modelValue, 1)" placeholder="请输入" />
                        <span>{{ "'" }}</span>
                    </div>

                </div>
            </div>
        </div>
        <div v-else class="number2_dufenmiao">
            <div class="number2_dufenmiao_index" style="margin-bottom: 13px;">
                <div class="number2_dufenmiao_index_jingdu"><input type="number" class="input_number" :value="longitude"
                        @blur="update($event, modelValue, 0)" @keydown.enter="update($event, modelValue, 0)"
                        step="0.0000001" placeholder="请输入经度" /><span>{{ '°' }}</span></div>
            </div>
            <div class="number2_dufenmiao_index" style="margin-bottom: 6px;">
                <div class="number2_dufenmiao_index_jingdu"><input type="number" class="input_number" :value="latitude"
                        @blur="update($event, modelValue, 1)" @keydown.enter="update($event, modelValue, 1)"
                        step="0.0000001" placeholder="请输入纬度" /><span>{{ '°' }}</span></div>
            </div>
        </div>
        <div class="checkbox1">
            <div v-if="!readonly && defaultValue !== undefined && ((modelValue[0] !== defaultValue[0]) || (modelValue[1] !== defaultValue[1]))"
                class="checkbox" @click.stop.prevent="resetDefult" @mouseover="mouseFlag = true"
                @mouseout="mouseFlag = false">
                <es-icon :name="'huaban'" :color="mouseFlag ? 'rgba(230, 230, 230, 1)' : 'rgba(230, 230, 230, 0.4)'"
                    :size="14" />
            </div>
        </div>
    </div>
    <div v-else class="number2_prop">
        <div class="number2_normal">
            <div class="number2_dufenmiao_index_jingdu" v-for="(_, index) in modelValue">
                <input :key="index" type="number" class="input_number" :disabled="readonly" :value="modelValue[index]"
                    @input="inputHandler($event, modelValue, index)" />
            </div>
        </div>
        <div class="checkbox1">
            <div v-if="!readonly && defaultValue !== undefined && ((modelValue[0] !== defaultValue[0]) || (modelValue[1] !== defaultValue[1]))"
                class="checkbox" @click.stop.prevent="resetDefult" @mouseover="mouseFlag = true"
                @mouseout="mouseFlag = false">
                <es-icon :name="'huaban'" :color="mouseFlag ? 'rgba(230, 230, 230, 1)' : 'rgba(230, 230, 230, 0.4)'"
                    :size="14" />
            </div>
        </div>
    </div>


</template>

<style scoped>
.number2_prop {
    width: 100%;
    height: 100%;

    display: flex;
    align-items: flex-start;
    justify-content: center;

}

.position_name {
    width: 70px;
    line-height: 26px;
}

.checkbox1 {
    width: 20px;
    height: 20px;
    box-sizing: border-box;
    margin-left: 10px;
    margin-top: 3px;
}

.checkbox {
    width: 20px;
    height: 20px;
    cursor: pointer;
    border: 1px solid rgba(230, 230, 230, 0.4);
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
}

.number2_dufenmiao {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.number2_normal {
    flex: 1;
    display: flex;
}

.number2_normal>.number2_dufenmiao_index_jingdu {
    width: 100%;
    height: 20px;
    display: flex;
}

.number2_dufenmiao>.number2_dufenmiao_index {
    width: 100%;
    height: 20px;
    display: flex;
}

.number2_dufenmiao>.number2_dufenmiao_index>label {
    display: inline-block;
    width: 70px;
    color: #fff;
    font-size: 14px;
    color: rgba(230, 230, 230, 1);
    font-size: 14px;
}

.number2_dufenmiao>.number2_dufenmiao_index>.number2_dufenmiao_index_jingdu {
    flex: 1;
    display: flex;
    position: relative;

}

.number2_dufenmiao>.number2_dufenmiao_index>.number2_dufenmiao_index_jingdu>span,
.number2_div>span {
    position: absolute;
    right: 0;
    top: 0;
    display: inline-block;
    width: 12px;
    height: 26px;
    line-height: 26px;
    text-align: center;
    color: rgba(255, 255, 255, 0.5);

}

.number2_dufenmiao_index_jingdu>.input_number {
    width: 100%;
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px;
    box-sizing: border-box;
}

.number2_dufenmiao_index_jingdu>.number2_div {
    flex: 1;
    position: relative;

}

.number2_dufenmiao_index_jingdu>.number2_div>input {
    width: 100%;
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding-left: 11px;
    box-sizing: border-box;
}

.number2_dufenmiao_index_jingdu>span {
    display: inline-block;
    width: 8px
}

.number2_dufenmiao_index_jingdu>.input_number:hover {
    border: 1px solid rgba(44, 104, 247, 1);
}

.number2_dufenmiao_index_jingdu>.input_number:focus {
    border: 1px solid #2c68f7;
}


.input_number::-webkit-outer-spin-button,
.input_number::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

.input_number[type="number"] {
    -moz-appearance: textfield;
}
</style>
